<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns#">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>String &middot; pfan</title>
        <meta name="description" content="String 方法  String.fromCharCode() //通过一串 Unicode 创建字符串。 String.fromCodePoint() //通过一串 码点 创建字符串。 String.raw() //通过模板字符串创建字符串。  实例方法  charAt(i) charCodeAt(i) codePointAt(i) concat(str) endsWith(str) includes(str) indexOf(str) lastIndexOf(str) localeCompare() match(regex) normalize() padEnd() padStart() repeat() replace(str1, str2) search(str) slice(begin, end) split(separator) startsWith(str) substring() toLocaleLowerCase() toLocaleUpperCase() toLowerCase() toString() toUpperCase() trim() trimEnd() trimStart() valueOf()  charAt  返回指定位置的字符,且返回的字符长度为 1，js 没有一种有别于字符串类型的字符数据类型
 string.charAt(index); &#34;porhub&#34;.charAt(0); //&#39;p&#39; &#34;porhub&#34;.charAt(1); //&#39;o&#39; &#34;porhub&#34;.charAt(2); //&#39;r&#39; charCodeAt  返回在指定的位置的字符的 Unicode 编码
 字符串中第一个字符的下标是 0。如果 index 是负数，或大于等于字符串的长度，则 charCodeAt() 返回 NaN。">
        <meta name="HandheldFriendly" content="True">
        <meta name="MobileOptimized" content="320">
        <meta name="generator" content="Hugo 0.73.0" />
        <meta name="robots" content="index,follow">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta property="og:title" content="String">
<meta property="og:description" content="String 方法  String.fromCharCode() //通过一串 Unicode 创建字符串。 String.fromCodePoint() //通过一串 码点 创建字符串。 String.raw() //通过模板字符串创建字符串。  实例方法  charAt(i) charCodeAt(i) codePointAt(i) concat(str) endsWith(str) includes(str) indexOf(str) lastIndexOf(str) localeCompare() match(regex) normalize() padEnd() padStart() repeat() replace(str1, str2) search(str) slice(begin, end) split(separator) startsWith(str) substring() toLocaleLowerCase() toLocaleUpperCase() toLowerCase() toString() toUpperCase() trim() trimEnd() trimStart() valueOf()  charAt  返回指定位置的字符,且返回的字符长度为 1，js 没有一种有别于字符串类型的字符数据类型
 string.charAt(index); &#34;porhub&#34;.charAt(0); //&#39;p&#39; &#34;porhub&#34;.charAt(1); //&#39;o&#39; &#34;porhub&#34;.charAt(2); //&#39;r&#39; charCodeAt  返回在指定的位置的字符的 Unicode 编码
 字符串中第一个字符的下标是 0。如果 index 是负数，或大于等于字符串的长度，则 charCodeAt() 返回 NaN。">
<meta property="og:type" content="article">
<meta property="og:url" content="https://blog.steps.info/2018/05/09/string/">
        <link rel="stylesheet" href="https://blog.steps.info/dist/site.css">
        <link rel="stylesheet" href="https://blog.steps.info/dist/syntax.css">
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300&subset=latin,cyrillic-ext,latin-ext,cyrillic">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
        
        
        
        
    </head>
    <body>
        
<script type="application/javascript">
var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
var doNotTrack = (dnt == "1" || dnt == "yes");
if (!doNotTrack) {
	(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
	(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
	m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
	})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
	ga('create', 'XXX', 'auto');
	ga('set', 'anonymizeIp', true);
	ga('send', 'pageview');
}
</script>


        <div id="wrapper">
            <header class="site-header">
                <div class="container">
                    <div class="site-title-wrapper">
                        
                            <h1 class="site-title">
                                <a title="Homepage" href="https://blog.steps.info/">Homepage</a>
                            </h1>
                        
                        <a class="button-square" href="https://blog.steps.info/index.xml"><i class="fa fa-rss"></i></a>
                        
                            <a class="button-square button-social hint--top" data-hint="Twitter" title="Twitter" href="https://twitter.com/pangel3" rel="me">
                                <i class="fa fa-twitter"></i>
                            </a>
                        
                        
                        
                            <a class="button-square button-social hint--top" data-hint="Gitlab" title="Gitlab" href="https://gitlab.com/funnypan" rel="me">
                                <i class="fa fa-gitlab"></i>
                            </a>
                        
                        
                            <a class="button-square button-social hint--top" data-hint="Github" title="Github" href="https://github.com/RazzilDarkbrew" rel="me">
                                <i class="fa fa-github-alt"></i>
                            </a>
                        
                        
                            <a class="button-square button-social hint--top" data-hint="Stack Overflow" title="Stack Overflow" href="https://stackoverflow.com/users/7784704/stepsinfo" rel="me">
                                <i class="fa fa-stack-overflow"></i>
                            </a>
                        
                        
                        
                            <a class="button-square button-social hint--top" data-hint="Email" title="Email" href="mailto:gemini0525@foxmail.com">
                                <i class="fa fa-envelope"></i>
                            </a>
                        
                    </div>

                    <ul class="site-nav">
                        
                    </ul>
                </div>
            </header>

            <div id="container">


<div class="container">
    <article class="post-container" itemscope="" itemtype="http://schema.org/BlogPosting">
        <header class="post-header">
    <h1 class="post-title" itemprop="name headline">String</h1>
    
    <p class="post-date">
        <span>Published <time datetime="2018-05-09" itemprop="datePublished">Wed, May 9, 2018</time></span>
        <span>by</span>
        <span itemscope="" itemprop="author" itemtype="https://schema.org/Person">
            <span itemprop="name">
                <a href="https://github.com/RazzilDarkbrew" itemprop="url" rel="author">pfan</a>
            </span>
        </span>
    </p>
    
        <p class="post-reading post-line">
            <span>Estimated reading time: 2 min</span>
        </p>
    
</header>

        <div class="post-content clearfix" itemprop="articleBody">
    

    <h2 id="string">String</h2>
<h3 id="方法">方法</h3>
<ul>
<li>String.fromCharCode() //通过一串 Unicode 创建字符串。</li>
<li>String.fromCodePoint() //通过一串 码点 创建字符串。</li>
<li>String.raw() //通过模板字符串创建字符串。</li>
</ul>
<h3 id="实例方法">实例方法</h3>
<ul>
<li>charAt(i)</li>
<li>charCodeAt(i)</li>
<li>codePointAt(i)</li>
<li>concat(str)</li>
<li>endsWith(str)</li>
<li>includes(str)</li>
<li>indexOf(str)</li>
<li>lastIndexOf(str)</li>
<li>localeCompare()</li>
<li>match(regex)</li>
<li>normalize()</li>
<li>padEnd()</li>
<li>padStart()</li>
<li>repeat()</li>
<li>replace(str1, str2)</li>
<li>search(str)</li>
<li>slice(begin, end)</li>
<li>split(separator)</li>
<li>startsWith(str)</li>
<li>substring()</li>
<li>toLocaleLowerCase()</li>
<li>toLocaleUpperCase()</li>
<li>toLowerCase()</li>
<li>toString()</li>
<li>toUpperCase()</li>
<li>trim()</li>
<li>trimEnd()</li>
<li>trimStart()</li>
<li>valueOf()</li>
</ul>
<h3 id="charat">charAt</h3>
<blockquote>
<p>返回指定位置的字符,且返回的字符长度为 1，<strong><em>js 没有一种有别于字符串类型的字符数据类型</em></strong></p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">string</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="nx">index</span><span class="p">);</span>

<span class="s2">&#34;porhub&#34;</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="c1">//&#39;p&#39;
</span><span class="c1"></span><span class="s2">&#34;porhub&#34;</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> <span class="c1">//&#39;o&#39;
</span><span class="c1"></span><span class="s2">&#34;porhub&#34;</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span> <span class="c1">//&#39;r&#39;
</span></code></pre></div><h3 id="charcodeat">charCodeAt</h3>
<blockquote>
<p>返回在指定的位置的字符的 Unicode 编码</p>
</blockquote>
<p><strong>字符串中第一个字符的下标是 0。如果 index 是负数，或大于等于字符串的长度，则 charCodeAt() 返回 NaN。</strong></p>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">string</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="nx">index</span><span class="p">);</span>

<span class="s2">&#34;porhub&#34;</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="c1">//112
</span><span class="c1"></span><span class="s2">&#34;porhub&#34;</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> <span class="c1">//111
</span><span class="c1"></span><span class="s2">&#34;porhub&#34;</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span> <span class="c1">//114
</span></code></pre></div><h3 id="codepointat">codePointAt</h3>
<blockquote>
<p>ES2015 引入的，用于处理 2 UTF-16 字符的，比如中文，在 ES2015 之前 charCodeAt 只能处理单个字符，处理双字符必须一个个处理然后拼接。</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">string</span><span class="p">.</span><span class="nx">codePointAt</span><span class="p">(</span><span class="nx">index</span><span class="p">);</span>

<span class="c1">//ES2015之前
</span><span class="c1"></span><span class="s2">&#34;𠮷&#34;</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="mi">0</span><span class="p">).</span><span class="nx">toString</span><span class="p">(</span><span class="mi">16</span><span class="p">);</span> <span class="c1">//d842
</span><span class="c1"></span><span class="s2">&#34;𠮷&#34;</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nx">toString</span><span class="p">(</span><span class="mi">16</span><span class="p">);</span> <span class="c1">//dfb7
</span><span class="c1"></span><span class="p">(</span><span class="s2">&#34;\ud842\udfb7&#34;</span><span class="p">);</span> <span class="c1">//𠮷
</span><span class="c1"></span>
<span class="c1">//ES2015
</span><span class="c1"></span><span class="s2">&#34;𠮷&#34;</span><span class="p">.</span><span class="nx">codePointAt</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="c1">//20bb7
</span><span class="c1"></span><span class="p">(</span><span class="s2">&#34;\u{20bb7}&#34;</span><span class="p">);</span> <span class="c1">//𠮷
</span></code></pre></div><h3 id="concat">concat</h3>
<blockquote>
<p>类似 Array 的 concat</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">stringObject</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">stringX</span><span class="p">,</span><span class="nx">stringX</span><span class="p">,...,</span><span class="nx">stringX</span><span class="p">)</span>

<span class="s2">&#34;browser&#34;</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="s2">&#34;porhub&#34;</span><span class="p">);</span>
</code></pre></div><p>通常使用 + 拼接来得更方便，或者使用 ES2015 的``字符扩展。</p>
<h3 id="endwith">endWith</h3>
<blockquote>
<p>用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的，根据判断结果返回 true 或 false。</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">endsWith</span><span class="p">(</span><span class="nx">searchString</span><span class="p">,</span> <span class="nx">position</span><span class="p">);</span>

<span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">endsWith</span><span class="p">(</span><span class="s2">&#34;Script&#34;</span><span class="p">);</span> <span class="c1">//true
</span><span class="c1"></span><span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">endsWith</span><span class="p">(</span><span class="s2">&#34;script&#34;</span><span class="p">);</span> <span class="c1">//false
</span><span class="c1"></span><span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">endsWith</span><span class="p">(</span><span class="s2">&#34;Script&#34;</span><span class="p">,</span> <span class="mi">5</span><span class="p">);</span> <span class="c1">//false
</span><span class="c1"></span><span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">endsWith</span><span class="p">(</span><span class="s2">&#34;as&#34;</span><span class="p">,</span> <span class="mi">5</span><span class="p">);</span> <span class="c1">//true
</span></code></pre></div><h3 id="includes">includes</h3>
<blockquote>
<p>用于判断一个字符串是否包含在另一个字符串中，根据情况返回 true 或 false</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">includes</span><span class="p">(</span><span class="nx">searchString</span><span class="p">,</span> <span class="nx">position</span><span class="p">);</span>

<span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">includes</span><span class="p">(</span><span class="s2">&#34;Script&#34;</span><span class="p">);</span> <span class="c1">//true
</span><span class="c1"></span><span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">includes</span><span class="p">(</span><span class="s2">&#34;script&#34;</span><span class="p">);</span> <span class="c1">//false
</span><span class="c1"></span><span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">includes</span><span class="p">(</span><span class="s2">&#34;nice&#34;</span><span class="p">);</span> <span class="c1">//true
</span><span class="c1"></span><span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">includes</span><span class="p">(</span><span class="s2">&#34;nice&#34;</span><span class="p">,</span> <span class="mi">3</span><span class="p">);</span> <span class="c1">//false
</span></code></pre></div><h3 id="indexof">indexOf</h3>
<blockquote>
<p>用于返回字符串中出现某个字符的初始位置，如果没有找到，返回-1.</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">searchString</span><span class="p">,</span> <span class="nx">startPosition</span><span class="p">);</span>

<span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s2">&#34;a&#34;</span><span class="p">);</span> <span class="c1">//1
</span><span class="c1"></span><span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s2">&#34;a&#34;</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span> <span class="c1">//3
</span></code></pre></div><h3 id="lastindexof">lastIndexOf</h3>
<blockquote>
<p>用于返回某个字符最后出现的位置，如果没有，返回-1</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">lastIndexOf</span><span class="p">(</span><span class="nx">searchString</span><span class="p">,</span> <span class="nx">startPosition</span><span class="p">);</span>

<span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">lastIndexOf</span><span class="p">(</span><span class="s2">&#34;a&#34;</span><span class="p">);</span> <span class="c1">//3
</span><span class="c1"></span><span class="s2">&#34;JavaScript&#34;</span><span class="p">.</span><span class="nx">lastIndexOf</span><span class="p">(</span><span class="s2">&#34;a&#34;</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="c1">//1
</span></code></pre></div><h3 id="padend">padEnd</h3>
<blockquote>
<p>用一个字符串填充当前字符串（如果需要的话则重复填充），返回填充后达到指定长度的字符串。从当前字符串的末尾（右侧）开始填充。</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">padEnd</span><span class="p">(</span><span class="nx">targetLength</span><span class="p">,</span> <span class="nx">string</span><span class="p">);</span>

<span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">padEnd</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span> <span class="c1">// &#34;abc       &#34;
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">padEnd</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="s2">&#34;foo&#34;</span><span class="p">);</span> <span class="c1">// &#34;abcfoofoof&#34;
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">padEnd</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="s2">&#34;123456&#34;</span><span class="p">);</span> <span class="c1">// &#34;abc123&#34;
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">padEnd</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> <span class="c1">// &#34;abc&#34;
</span></code></pre></div><h3 id="padstart">padStart</h3>
<blockquote>
<p>用另一个字符串填充当前字符串(重复，如果需要的话)，以便产生的字符串达到给定的长度。填充从当前字符串的开始(左侧)应用的</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">padStart</span><span class="p">(</span><span class="nx">targetLength</span><span class="p">,</span> <span class="nx">string</span><span class="p">);</span>

<span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">padStart</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span> <span class="c1">// &#34;       abc&#34;
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">padStart</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="s2">&#34;foo&#34;</span><span class="p">);</span> <span class="c1">// &#34;foofoofabc&#34;
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">padStart</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="s2">&#34;123465&#34;</span><span class="p">);</span> <span class="c1">// &#34;123abc&#34;
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">padStart</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="s2">&#34;0&#34;</span><span class="p">);</span> <span class="c1">// &#34;00000abc&#34;
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">padStart</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> <span class="c1">// &#34;abc&#34;
</span></code></pre></div><h3 id="repeat">repeat</h3>
<blockquote>
<p>构造并返回一个包含被连接在一起的指定数量的新字符串。</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">repeat</span><span class="p">(</span><span class="nx">count</span><span class="p">);</span>

<span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">repeat</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span> <span class="c1">// RangeError: repeat count must be positive and less than inifinity
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">repeat</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="c1">// &#34;&#34;
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">repeat</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> <span class="c1">// &#34;abc&#34;
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">repeat</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span> <span class="c1">// &#34;abcabc&#34;
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">repeat</span><span class="p">(</span><span class="mf">3.5</span><span class="p">);</span> <span class="c1">// &#34;abcabcabc&#34; 参数count将会被自动转换成整数.
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">repeat</span><span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="mi">0</span><span class="p">);</span> <span class="c1">// RangeError: repeat count must be positive and less than inifinity
</span></code></pre></div><h3 id="slice">slice</h3>
<blockquote>
<p>截取字符串的一部分，并返回一个新的字符串</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">startPosition</span><span class="p">,</span> <span class="nx">endPosition</span><span class="p">);</span>

<span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="c1">//a
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span> <span class="c1">//ab
</span></code></pre></div><h3 id="split">split</h3>
<blockquote>
<p>将字符串切割成数组</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="nx">separator</span><span class="p">,</span> <span class="nx">limit</span><span class="p">);</span>

<span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">split</span><span class="p">();</span> <span class="c1">//&#34;[a,b,c]&#34;;
</span><span class="c1"></span><span class="s2">&#34;a,b,c&#34;</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s2">&#34;,&#34;</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="c1">//[a]
</span></code></pre></div><h3 id="startwith">startWith</h3>
<blockquote>
<p>判断字符串是否以给定的字符串开始，返回 true/false</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">startsWith</span><span class="p">(</span><span class="nx">searchString</span><span class="p">,</span> <span class="nx">poistion</span><span class="p">);</span>

<span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">startsWith</span><span class="p">(</span><span class="s2">&#34;a&#34;</span><span class="p">);</span> <span class="c1">//true
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">startsWith</span><span class="p">(</span><span class="s2">&#34;a&#34;</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="c1">//false
</span></code></pre></div><h3 id="substring">substring</h3>
<blockquote>
<p>根据开始索引到结束索引截取字符串</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="nx">startPosition</span><span class="p">,</span> <span class="nx">endPosition</span><span class="p">);</span>

<span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="c1">//a
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span> <span class="c1">//abc
</span></code></pre></div><ol>
<li>如果 startPosition 等于 indexEnd，substring 返回一个空字符串。</li>
<li>如果省略 indexEnd，substring 提取字符一直到字符串末尾。</li>
<li>如果任一参数小于 0 或为 NaN，则被当作 0。</li>
<li>如果任一参数大于 stringName.length，则被当作 stringName.length。</li>
<li>如果 startPosition 大于 indexEnd，则 substring 的执行效果就像两个参数调换了一样。见下面的例子。</li>
</ol>
<h3 id="tolocalelowercasetolocaleuppercasetouppercasetolowercase">toLocaleLowerCase/toLocaleUpperCase/toUpperCase/toLowerCase</h3>
<blockquote>
<p>将字符串转换成小写/大写</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">toLocaleLowerCase</span><span class="p">()</span> <span class="o">/</span> <span class="nx">str</span><span class="p">.</span><span class="nx">toLocaleUpperCase</span><span class="p">()</span> <span class="o">/</span> <span class="nx">str</span><span class="p">.</span><span class="nx">toUpperCase</span><span class="p">()</span> <span class="o">/</span> <span class="nx">str</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">();</span>

<span class="s2">&#34;ABC&#34;</span><span class="p">.</span><span class="nx">toLocaleLowerCase</span><span class="p">();</span> <span class="c1">//abc
</span><span class="c1"></span><span class="s2">&#34;abc&#34;</span><span class="p">.</span><span class="nx">toLocaleUpperCase</span><span class="p">();</span> <span class="c1">//ABC
</span></code></pre></div><h3 id="tostring">toString</h3>
<blockquote>
<p>将*转换成字符串</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">String</span><span class="p">(</span><span class="s2">&#34;Hello world&#34;</span><span class="p">);</span>

<span class="nx">alert</span><span class="p">(</span><span class="nx">x</span><span class="p">.</span><span class="nx">toString</span><span class="p">());</span> <span class="c1">// 输出 &#34;Hello world&#34;
</span></code></pre></div><h3 id="trimtrimstarttrimend">trim/trimStart/trimEnd</h3>
<blockquote>
<p>移除空白字符/起始位置空白字符/结尾位置空白字符</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">trim</span><span class="p">()</span> <span class="o">/</span> <span class="nx">str</span><span class="p">.</span><span class="nx">trimStart</span><span class="p">()</span> <span class="o">/</span> <span class="nx">str</span><span class="p">.</span><span class="nx">trimEnd</span><span class="p">();</span>

<span class="s2">&#34;Testing&#34;</span><span class="p">.</span><span class="nx">trim</span><span class="p">();</span> <span class="c1">//&#39;Testing&#39;
</span><span class="c1"></span><span class="s2">&#34; Testing&#34;</span><span class="p">.</span><span class="nx">trim</span><span class="p">();</span> <span class="c1">//&#39;Testing&#39;
</span><span class="c1"></span><span class="s2">&#34; Testing &#34;</span><span class="p">.</span><span class="nx">trim</span><span class="p">();</span> <span class="c1">//&#39;Testing&#39;
</span><span class="c1"></span><span class="s2">&#34;Testing &#34;</span><span class="p">.</span><span class="nx">trim</span><span class="p">();</span> <span class="c1">//&#39;Testing&#39;
</span></code></pre></div><h3 id="valueof">valueOf</h3>
<blockquote>
<p>返回 str 的原始值，等同于 String.prototype.toString</p>
</blockquote>
<div class="highlight"><pre class="chroma"><code class="language-javascript" data-lang="javascript"><span class="nx">str</span><span class="p">.</span><span class="nx">valueOf</span><span class="p">();</span>

<span class="nx">x</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">String</span><span class="p">(</span><span class="s2">&#34;Hello world&#34;</span><span class="p">);</span>
<span class="nx">alert</span><span class="p">(</span><span class="nx">x</span><span class="p">.</span><span class="nx">valueOf</span><span class="p">());</span> <span class="c1">// Displays &#34;Hello world&#34;
</span></code></pre></div><h3 id="扩展学习">扩展学习</h3>

</div>

        <footer class="post-footer clearfix">
    
        <p class="post-tags">
            <span>Tagged:</span>
            
            
                <a href="/tags/js/">js</a>
            
        </p>
    

    <div class="share">
        
            <a class="icon-twitter" href="https://twitter.com/share?text=String&url=https%3a%2f%2fblog.steps.info%2f2018%2f05%2f09%2fstring%2f"
                onclick="window.open(this.href, 'twitter-share', 'width=550,height=235');return false;">
                <i class="fa fa-twitter"></i>
                <span class="hidden">Twitter</span>
            </a>
        

        
            <a class="icon-facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3a%2f%2fblog.steps.info%2f2018%2f05%2f09%2fstring%2f"
                onclick="window.open(this.href, 'facebook-share','width=580,height=296');return false;">
                <i class="fa fa-facebook"></i>
                <span class="hidden">Facebook</span>
            </a>
        

        
            <a class="icon-google-plus" href="https://plus.google.com/share?url=https%3a%2f%2fblog.steps.info%2f2018%2f05%2f09%2fstring%2f"
              onclick="window.open(this.href, 'google-plus-share', 'width=490,height=530');return false;">
              <i class="fa fa-google-plus"></i>
                <span class="hidden">Google+</span>
            </a>
        
        
    </div>
</footer>

        
    <div class="comments">
        
    </div>

    </article>
</div>

            </div>
        </div>

        <footer class="footer">
            <div class="container">
                <div class="site-title-wrapper">
                    <h1 class="site-title">
                        <a title="Homepage" href="https://blog.steps.info/">Homepage</a>
                    </h1>
                    <a class="button-square button-jump-top js-jump-top" href="#">
                        <i class="fa fa-angle-up"></i>
                    </a>
                </div>

                <p class="footer-copyright">
                    <span>&copy; 2020 / Powered by <a href="https://gohugo.io/">Hugo</a></span>
                </p>
                <p class="footer-copyright">
                    <span><a href="https://github.com/roryg/ghostwriter">Ghostwriter theme</a> By <a href="http://jollygoodthemes.com">JollyGoodThemes</a></span>
                    <span>/ <a href="https://github.com/jbub/ghostwriter">Ported</a> to Hugo By <a href="https://github.com/jbub">jbub</a></span>
                </p>
            </div>
        </footer>

        <script src="https://blog.steps.info/js/jquery-1.11.3.min.js"></script>
        <script src="https://blog.steps.info/js/jquery.fitvids.js"></script>
        <script src="https://blog.steps.info/js/scripts.js"></script>
    </body>
</html>

